package com.cpe.config;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.wechat.api.WechatAPI;
import com.wechat.data.WechatEnv;
import com.wechat.data.WechatReply;
import com.wechat.data.message.reply.article.Item;

import jodd.util.StringPool;

@Configuration
@PropertySource({ "classpath:wechat.properties" })
public class WechatConfig {

	@Resource
	private Environment environment;

	@Bean
	public WechatEnv wechatEnv() {
		WechatEnv wechatEnv = new WechatEnv();
		wechatEnv.setAppid(environment.getProperty("wechat.appid"));
		wechatEnv.setAppsecret(environment.getProperty("wechat.secret"));
		wechatEnv.setToken(environment.getProperty("wechat.token"));
		return wechatEnv;
	}

	@Bean
	public WechatReply wechatReply() throws UnsupportedEncodingException {
		WechatReply wechatReply = new WechatReply();
		String subscribe = environment.getProperty("wechat.reply.subscribe");
		// 回复消息：关注事件
		wechatReply.setSubscribe(new String(subscribe.getBytes(StringPool.ISO_8859_1), StringPool.UTF_8));
		// 回复消息：关键字->成绩
		String chengjiTitle = environment.getProperty("wechat.reply.chengji.title");
		String chengjiDesc = environment.getProperty("wechat.reply.chengji.desc");
		String chengjiPicUrl = environment.getProperty("wechat.reply.chengji.picurl");
		String chengjiUrl = environment.getProperty("wechat.reply.chengji.url");
		Item chengji = new Item();
		chengji.setTitle(new String(chengjiTitle.getBytes(StringPool.ISO_8859_1), StringPool.UTF_8));
		chengji.setDescription(new String(chengjiDesc.getBytes(StringPool.ISO_8859_1), StringPool.UTF_8));
		chengji.setPicUrl(chengjiPicUrl);
		chengji.setUrl(chengjiUrl);
		wechatReply.setChengji(chengji);
		return wechatReply;
	}

	@Bean
	public WechatAPI wechatAPI() {
		return new WechatAPI(wechatEnv(), true);
	}
}
